package cn.cool.infrastructure.gateway;

import cn.cool.types.enums.ResponseCode;
import cn.cool.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class GroupBuyNotifyService {
    @Resource
    private OkHttpClient httpClient;

    public String groupBuyNotify(String apiUrl, String notifyRequestDTOJson) throws Exception {
        try {
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, notifyRequestDTOJson);
            Request request = new Request.Builder().url(apiUrl)
                    .post(body)
                    .addHeader("content-type", "application/json")
                    .build();

            Response response = httpClient.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            throw new AppException(ResponseCode.HTTP_EXCEPTION);
        }
    }
}
